package com.wangrui.us3.s3;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;

import java.time.Duration;

public class Test2 {

    private static final Logger logger = LoggerFactory.getLogger(Test2.class);
    public static void main(String[] args) {
        String bucketName = "test-gboss-kj";
        String keyName = "万相2.5_1759110999134.mp4";
        System.out.println(createPresignedGetUrl(bucketName,keyName));
    }

    /* Create a pre-signed URL to download an object in a subsequent GET request. */
    public static String createPresignedGetUrl(String bucketName, String keyName) {
        try (S3Presigner presigner = S3Util.getPreDesigner()) {

            GetObjectRequest objectRequest = GetObjectRequest.builder()
                    .bucket(bucketName)
                    .key(keyName)
                    .build();

            GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder()
                    .signatureDuration(Duration.ofMinutes(10))  // The URL will expire in 10 minutes.
                    .getObjectRequest(objectRequest)
                    .build();

            PresignedGetObjectRequest presignedRequest = presigner.presignGetObject(presignRequest);
            logger.info("Presigned URL: [{}]", presignedRequest.url().toString());
            logger.info("HTTP method: [{}]", presignedRequest.httpRequest().method());

            return presignedRequest.url().toExternalForm();
        }
    }
}
